package net.jcip.examples;

/**
 * BarrierTimer
 * <p/>
 * Barrier-based timer
 *
 * @author Brian Goetz and Tim Peierls
 */
public class BarrierTimer implements Runnable {
    /**
	 * @uml.property  name="started"
	 */
    private boolean started;
    /**
	 * @uml.property  name="startTime"
	 */
    private long startTime;
	/**
	 * @uml.property  name="endTime"
	 */
	private long endTime;

    public synchronized void run() {
        long t = System.nanoTime();
        if (!started) {
            started = true;
            startTime = t;
        } else
            endTime = t;
    }

    public synchronized void clear() {
        started = false;
    }

    public synchronized long getTime() {
        return endTime - startTime;
    }
}
